package org.iks.feign.config;

import io.github.taikonaut3.virtue.config.Invocation;
import io.github.taikonaut3.virtue.config.filter.Filter;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import static org.iks.common.constant.Constant.AUTHENTICATION;

/**
 * @Author WenBo Zhou
 * @Date 2024/3/11 15:59
 */
public class TokenFilter implements Filter {

    @Override
    public Object doFilter(Invocation invocation) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            String token = request.getHeader(AUTHENTICATION);
            invocation.url().addParameter(AUTHENTICATION, token);
        }
        return invocation.invoke();
    }
}
